[RAA-8779]-[TH]-[Added in script to resolve all reference after Redoc…#2826
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a build-time Python script to post-process the bundled OpenAPI JSON, resolving any remaining {"$ref": ".../*.json"} objects that Redocly v2 leaves inside example payloads, and wires it into the npm run publish pipeline so the generated build/e-referrals-service-api.json contains fully inlined examples.
Changes:
- Added
scripts/resolve_example_refs.pyto replace remaining JSON-file$refobjects (primarily undercomponents/{stu3,r4}/examples) with file contents during publishing. - Updated
package.jsonpublishscript to run the new resolver betweenredocly bundleand existingset_version.py/populate_placeholders.py.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| scripts/resolve_example_refs.py | New stdin→stdout transformer to inline example JSON $refs after bundling. |
| package.json | Inserts the resolver into the publish pipeline. |
Comment on lines
+5
to
+16
| Reads a bundled OpenAPI spec on stdin and resolves any remaining $ref | ||
| objects found inside example 'value' fields. This is needed because | ||
| Redocly CLI v2 no longer dereferences $ref inside example values | ||
| during bundling. | ||
|
|
||
| The script walks the entire spec looking for objects of the form: | ||
| {"$ref": "some/path/to/file.json"} | ||
|
|
||
| that appear inside example value contexts, loads the referenced JSON | ||
| file from disk relative to the specification directory, and replaces | ||
| the $ref object with the actual file content. | ||
|
|
Comment on lines
+50
to
+54
| # Try both stu3 and r4 directories | ||
| for subdir in ["stu3", "r4"]: | ||
| candidate = os.path.join(SPEC_COMPONENTS_DIR, subdir, relative_from_examples) | ||
| if os.path.isfile(candidate): | ||
| return candidate |
Comment on lines
+59
to
+83
| def resolve_refs(obj): | ||
| """ | ||
| Recursively walk the parsed JSON object. When we find a dict that | ||
| is exactly {"$ref": "<path>"} where the path points to a .json | ||
| file, load that file and return its content instead. | ||
| """ | ||
| if isinstance(obj, dict): | ||
| if list(obj.keys()) == ["$ref"] and obj["$ref"].endswith(".json"): | ||
| ref_path = obj["$ref"] | ||
| abs_path = resolve_ref_path(ref_path) | ||
| if abs_path: | ||
| with open(abs_path, "r") as f: | ||
| return json.load(f) | ||
| else: | ||
| print( | ||
| f"Warning: Could not resolve $ref: {ref_path}", | ||
| file=sys.stderr, | ||
| ) | ||
| return obj | ||
| else: | ||
| return {k: resolve_refs(v) for k, v in obj.items()} | ||
| elif isinstance(obj, list): | ||
| return [resolve_refs(item) for item in obj] | ||
| else: | ||
| return obj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…ly update]-[GC]
Summary
Add any other relevant notes or explanations here. Remove this line if you have nothing to add.
Reviews Required
Review Checklist
ℹ️ This section is to be filled in by the reviewer.